import request from '@/utils/request'
import { getToken, setToken, removeToken } from '@/utils/auth'

import axios from 'axios'
import store from '@/store'

// axios.defaults.headers['token'] = getToken();

axios.interceptors.request.use(
  config => {
    if (store.getters.token) { // 判断是否存在token，如果存在的话，则每个http header都加上token
      config.headers.token = getToken()
    }
    return config
  },
  err => {
    return Promise.reject(err)
  })

export function loginByUsername(username, password) {
  return axios.post(`${process.env.BASE_API}/admin/index/login`, { username: username, password: password })
}

export function logout() {
  return axios.get(`${process.env.BASE_API}/admin/index/logout`)
}

export function getUserInfo() {
  // console.log(getToken());
  return axios.get(`${process.env.BASE_API}/admin/index/getUserInfo`)
}

